﻿#region License Info
//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion


using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ABMath.ModelFramework.Data;

namespace ExtraControlLibrary.Forms
{
    public partial class DateRangeSelectForm : Form
    {
        private readonly TimeSeries reference;

        public List<DateTime> GetTimeList()
        {
            bool skipWeekends = skipWeekendsCheckBox.Checked;
            var list = new List<DateTime>();
            if (!useReferenceCheckBox.Checked)
            {
                var current = dateTimePicker1.Value;
                var interval = new TimeSpan(0, 1, 0);
                if (radioButtonHours.Checked)
                    interval = new TimeSpan(1, 0, 0);
                if (radioButtonDays.Checked)
                    interval = new TimeSpan(1, 0, 0, 0);
                if (radioButtonWeeks.Checked)
                    interval = new TimeSpan(7, 0, 0, 0);

                while (current <= dateTimePicker2.Value)
                {
                    bool skipping = false;
                    if (skipWeekends)
                        if (current.DayOfWeek == DayOfWeek.Saturday || current.DayOfWeek == DayOfWeek.Sunday)
                            skipping = true;
                    if (!skipping)
                        list.Add(current);
                    current = new DateTime(current.Ticks + interval.Ticks);
                }
            }
            else
            {
                for (int t = 0; t < reference.Count; ++t)
                    list.Add(reference.TimeStamp(t));
            }
            return list;
        }

        public DateRangeSelectForm(TimeSeries referenceTS)
        {
            InitializeComponent();

            reference = referenceTS;
            if (referenceTS == null)
            {
                useReferenceCheckBox.Enabled = false;
                dateTimePicker1.Value = DateTime.Now;
                dateTimePicker2.Value = DateTime.Now.AddYears(1);
            }
            else
            {
                dateTimePicker1.Value = referenceTS.TimeStamp(0);
                dateTimePicker2.Value = referenceTS.TimeStamp(referenceTS.Count - 1);
            }
            radioButtonDays.Checked = true;
        }

        private void DisableControls()
        {
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            radioButtonDays.Enabled = false;
            radioButtonHours.Enabled = false;
            radioButtonWeeks.Enabled = false;
            skipWeekendsCheckBox.Enabled = false;
        }

        private void EnableControls()
        {
            dateTimePicker1.Enabled = true;
            dateTimePicker2.Enabled = true;
            radioButtonDays.Enabled = true;
            radioButtonHours.Enabled = true;
            radioButtonWeeks.Enabled = true;
            skipWeekendsCheckBox.Enabled = true;
        }

        private void useReferenceCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (useReferenceCheckBox.Checked)
                DisableControls();
            else
                EnableControls();
        }
    }
}